﻿using HandyControl.Controls;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Git提交规范
{
    /// <summary>
    /// Interaction logic for GitCommitToolWindowControl.
    /// </summary>
    public partial class GitCommitToolWindowControl : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GitCommitToolWindowControl"/> class.
        /// </summary>
        public GitCommitToolWindowControl()
        {
            this.InitializeComponent();
        }

        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            this.ScopeTextBox.Text = "";
            this.SubjectTextBox.Text = "";
            this.BodyTextBox.Text = "";
            Growl.Success("重置成功！");
        }

        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            string changeType = (ChangeTypeComboBox.SelectedItem as ComboBoxItem)?.Content.ToString().Split(' ')[0];
            string scope = ScopeTextBox.Text;
            string subject = SubjectTextBox.Text;
            string body = BodyTextBox.Text;
            if (string.IsNullOrEmpty(changeType) || string.IsNullOrEmpty(subject))
            {
                Growl.Warning("提交类型和主题是必填项，请填写完整");
                return;
            }

            StringBuilder commitMessage = new StringBuilder();
            commitMessage.Append($"{changeType}");

            if (!string.IsNullOrEmpty(scope))
            {
                commitMessage.Append($"({scope})");
            }

            commitMessage.Append($": {subject}");

            if (!string.IsNullOrEmpty(body))
            {
                commitMessage.Append($"\n\n{body}");
            }

            Clipboard.SetText(commitMessage.ToString());
            Growl.Success("已生成提交信息并复制到剪贴板！");
        }
    }
}